<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/11/21
 * Time: 10:49 AM
 */

namespace Biz\module\Decoupling\order;

use Biz\module\Decoupling\AbstractMain;
use Biz\service\Event;
use Doraemon\tools\Log as tLog;
use Biz\model\trade\OrderOperateLog as mOrderOperateLog;
use Biz\model\trade\OrderLogistics as mOrderLogistics;
use Biz\module\Classes\Order as clsOrder;
use Biz\model\trade\Order as mOrder;
use Doraemon\amqp\Queue as aQueue;

class ConfirmReceipt extends AbstractMain
{
    const NAME = '确认收货回调';

    public function main($message, $data)
    {

        $appendData = [
            'eventId' => $data['header']['event_id'],
            'uid' => $data['body']['uid'],
            'addTime' => $data['header']['addtime'],
            'orderNo' => $data['body']['order_no'],
            'isSys' => (int)$data['body']['is_sys'],
        ];

        $appendData['orderInfo'] = mOrder::getOrderInfo($appendData['orderNo']);

        $this->appendData($appendData);
    }

    protected function _exec_order_operate_log()
    {
        //确认收货的订单操作日志
        $result = mOrderOperateLog::createLog($this->data['orderInfo']['shop_id'], $this->data['orderNo'], mOrderOperateLog::TRADE_TYPE_ORDER, [
            'operate_type' => $this->data['isSys'] ? mOrderOperateLog::OPERATE_TYPE_AUTO_CONFIRM_RECEIPT : mOrderOperateLog::OPERATE_TYPE_USER_CONFIRM_RECEIPT,
            'operate_user_type' => $this->data['isSys'] ? mOrderOperateLog::OPERATE_USERTYPE_SYS : mOrderOperateLog::OPERATE_USERTYPE_USER,
            'operate_uid' => $this->data['isSys'] ? 0 : $this->data['uid'],
        ], '', $this->data['isSys'] ? '超时自动确认收货' : '确认收货','',[],$this->data['addTime']);

        $this->appendLog('增加操作日志',$this->data);

        if(is_object($result) && $result->rowCount() == 0){
            return false;
        }

    }

    protected function _exec_notice_logistics()
    {
        //通知物流信息已签收
        $result = mOrderLogistics::update(['status' => mOrderLogistics::STATUS_SUCCESS], [
            'order_no' => $this->data['orderNo'],
        ]);

        $this->appendLog('通知物流信息已签收',$this->data);


    }

    protected function _exec_confirm_order_settlement()
    {
        //订单结算流程
        clsOrder::afterConfirmOrderSettlement($this->data['orderInfo']['shop_id'], $this->data['orderInfo']['order_no'],
            $this->data['orderInfo']['pay_amount'], $this->data['orderInfo']['integral_amount'], $this->data['orderInfo']['coupon_amount'], $this->data['orderInfo']['activity_allowance'],
            $this->data['orderInfo']['packet_amount'],$this->data['orderInfo']['commissions'], $this->data['orderInfo']['pay_way'], $this->data['orderInfo']['shipping_amount'],
            $this->data['orderInfo']['confirm_time'], $this->data['isSys']);

        $this->appendLog('订单结算流程执行成功',$this->data);

    }
}